package com.guoyh.adapter.vp

import androidx.fragment.app.FragmentStatePagerAdapter
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager

/**
 * Created by GUO_YH on 2019/6/16 19:53
 */
class FragmentVPStateAdapter(
    private val fragmentManager: FragmentManager,
    private val fragments: List<Fragment>
) : FragmentStatePagerAdapter(
    fragmentManager,BEHAVIOR_SET_USER_VISIBLE_HINT
) {
    override fun getItem(i: Int): Fragment {
        return fragments[i]
    }

    override fun getCount(): Int {
        return fragments.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val fragment = super.instantiateItem(container, position) as Fragment
        fragmentManager.beginTransaction().show(fragment).commitAllowingStateLoss()
        return fragment
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        val fragment = fragments[position]
        fragmentManager.beginTransaction().hide(fragment).commitAllowingStateLoss()
    }
}